class VideosController < ApplicationController
  before_filter :require_user
	def index
    @video_ablum = VideoAlbum.find(params[:id])
		@videos = @video_ablum.videos.order("order_no ASC")
  end

  def show
    @video = Video.find(params[:id])
  end

  def new
    @video = Video.new
  end

  def create
    @video = Video.new(params[:video])
    @video.order_no = Video.last.blank? ? "0" : Video.last.id  
    @video.status = "1"
    if @video.save
			flash[:notice] = "Successfully created video."
      redirect_to video_albums_path 
    else
      render :action => 'new'
    end
  end

  def edit
    @video = Video.find(params[:id])
  end

  def update
    @video = Video.find(params[:id])
    if @video.update_attributes(params[:video])
      redirect_to @video, :notice  => "Successfully updated video."
    else
      render :action => 'edit'
    end
  end

  def change_position
    @video_1 = Video.find(params[:id])
    @video_2 = Video.find(params[:change_id])
    if !@video_1.blank? && !@video_2.blank?
      @video_1.order_no,@video_2.order_no = @video_2.order_no,@video_1.order_no
      @video_1.save
      @video_2.save
    end
    redirect_to :action => :index,:id => @video_1.video_album_id
  end

  def destroy
    @video = Video.find(params[:id])
    id = @video.video_album_id
    @video.destroy
    redirect_to :action=>:index,:id=>id, :notice => "Successfully destroyed video."
  end
end
